var mysql  = require('mysql');
var db_info= {
    host     : 'localhost',
    user     : 'root',
    password : 'abgd951753',
    port: '3306',
    database: 'chat'
};

function db_connect(callback,data=db_info){
    var connection = mysql.createConnection(data);
    connection.connect();
    callback(connection);
}

exports.insert_user= function(id,name,date,callback){
    db_connect(function(connection){
        var  addSql = 'INSERT INTO users(id,nickname,create_time) VALUES(?,?,?)';

        connection.query(addSql,[id,name,date],function (err, result) {
            if(err){
                console.log('[INSERT ERROR] - ',err.message);
                return;
            }
            if(callback){
                callback(result);
            }
            console.log('--------------------------INSERT----------------------------');
            //console.log('INSERT ID:',result.insertId);
            console.log('INSERT ID:',result);
            console.log('-----------------------------------------------------------------\n\n');
        });
        connection.end()
    })
}

exports.select_user= function(name,callback){
    db_connect(function(connection){
        var  addSql = 'select * from users where nickname=?';

        connection.query(addSql,[name],function (err, result) {
            if(err){
                console.log('[select ERROR] - ',err.message);
                return;
            }
            callback(result);

            console.log('--------------------------INSERT----------------------------');
            //console.log('INSERT ID:',result.insertId);
            console.log('select ID:',result);
            console.log('-----------------------------------------------------------------\n\n');
        });
        connection.end()
    })
}
exports.select_user_count= function(callback){
    db_connect(function(connection){
        var  addSql = 'select COUNT(*) as length from users';

        connection.query(addSql,function (err, result) {
            if(err){
                console.log('[select ERROR] - ',err.message);
                return;
            }
            callback(result);

            console.log('--------------------------INSERT----------------------------');
            //console.log('INSERT ID:',result.insertId);
            console.log('select ID:',result);
            console.log('-----------------------------------------------------------------\n\n');
        });
        connection.end()
    })
}

exports.insert_record= function(nickname,text,room,time,callback){
    db_connect(function(connection){
        var  addSql = 'INSERT INTO chat_records(nickname,text,room,time) VALUES(?,?,?,?)';

        connection.query(addSql,[nickname,text,room,time],function (err, result) {
            if(err){
                console.log('[INSERT ERROR] - ',err.message);
                return;
            }
            if(callback){
                callback(result);
            }

            console.log('--------------------------INSERT----------------------------');
            //console.log('INSERT ID:',result.insertId);
            console.log('INSERT ID:',result);
            console.log('-----------------------------------------------------------------\n\n');
        });
        connection.end()
    })
}

exports.select_record= function(name,callback){  //查询自己的输出记录
    db_connect(function(connection){
        var  addSql = 'select * from chat_records where nickname=?';

        connection.query(addSql,[name],function (err, result) {
            if(err){
                console.log('[select ERROR] - ',err.message);
                return;
            }
            callback(result);

            console.log('--------------------------INSERT----------------------------');
            //console.log('INSERT ID:',result.insertId);
            console.log('select ID:',result);
            console.log('-----------------------------------------------------------------\n\n');
        });
        connection.end()
    })
}
